package Smt.ServerImp;

import Smt.BaseDao.BillDao;
import Smt.BaseDaoImp.BillDaoImp;
import Smt.Server.BillServer;
import Smt.Util.Bill;

import java.util.ArrayList;

public class BillServerImp implements BillServer {
	BillDao ur = new BillDaoImp();
	private int yenum;
	private int BillCount;

	public int getBillCount() {
		return BillCount;
	}

	public void setBillCount(int billCount) {
		BillCount = billCount;
	}

	public int getYenum() {
		return yenum;
	}

	public void setYenum(int yenum) {
		this.yenum = yenum;
	}

	//查询账单列表
	public ArrayList<Bill> getBill() {
		Object[] o = null;
		String sql = "select * from bill ORDER by id";
		return ur.getBillList(o, sql);
	}

	//双条件账单总数
	public int BillCount(String name, String pay) {
		return bd.BillCount(name, pay);
	}

	public ArrayList<Bill> PayNameRead(int y, int ye, String pay, String name) {
		this.BillCount = BillCount(name, pay);
		this.yenum = BillCount / ye;//总页数
		if (BillCount % ye != 0)
			this.yenum++;
		int tou = (y - 1) * ye + 1;//头记录
		int wei = y * ye;//尾记录
		String sql = "select * from (select b.id,b.price,b.unit,b.num,b.description,b.pay,b.name bname,b.dates,s.name sname,@rownum:=@rownum+1 rn from bill b,supplier s,(select @rownum:=0) t where b.supplierno=s.id and b.pay like ? and b.name like ? ORDER by b.id) a where a.rn>=? and a.rn<=?";
		Object[] o = {"%" + pay + "%", "%" + name + "%", tou, wei};
		return ur.getBillList(o, sql);
	}

	//当前页de信息列表
	public ArrayList<Bill> read(int y, int ye) {//当前页，每页内容
		this.BillCount = BillCount("", "");
		this.yenum = BillCount / ye;//总页数
		if (BillCount % ye != 0)
			this.yenum++;
		int tou = (y - 1) * ye + 1;//头记录
		int wei = y * ye;//尾记录
		Object[] o = {tou, wei};
		String sql = "select * from (select b.id,b.price,b.unit,b.num,b.description,b.pay,b.name bname,b.dates,s.name sname,@rownum:=@rownum+1 rn from bill b,supplier s,(select @rownum:=0) t where b.supplierno=s.id ORDER by b.id) a where a.rn>=? and a.rn<=?";
		return ur.getBillList(o, sql);
	}

	//通讯录的增加
	public boolean add(Bill Bill) {
		String sql = "insert into bill(price,unit,num,description,pay,name,dates,supplierno) values(?,?,?,?,?,?,now(),?)";
		Object[] o = {Bill.getPrice(), Bill.getUnit(), Bill.getNum(), Bill.getDescription(), Bill.getPay(), Bill.getName(), Bill.getS_id()};
		return bd.util(sql, o);
	}

	//通讯录的删除
	public boolean remove(int id) {
		Object[] o = {id};
		String sql = "delete from Bill where id=?";
		return bd.util(sql, o);
	}

	//通讯录的更改
	public boolean update(Bill Bill) {

		String sql = "update Bill set price=?,unit=?,num=?,description=?,pay=?,name=?,supplierno=? where id=?";
		Object[] o = {Bill.getPrice(), Bill.getUnit(), Bill.getNum(), Bill.getDescription(), Bill.getPay(), Bill.getName(), Bill.getS_id(), Bill.getId()};
		return bd.util(sql, o);
	}
}
